<?php
class PostCommentsController extends Controller {
	public function index() {
		$this->getSmarty ();
		// $this->smarty->assign ( 'page_no', $page_no );
		// $this->smarty->assign ( 'page_html', $page_html );
		$this->smarty->display ( "admin/posts/postcomments.tpl" );
	}
	public function checkUser() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'PostCommentsService.class.php';
		$service = new UserService ( $this->getDB () );
		$name = $_POST ["name"];
	}
	public function getallrole() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'PostCommentsService.class.php';
		$service = new PostCommentsService ( $this->getDB () );
		$roles = $service->getallrole ();
		echo json_encode ( $roles );
	}
	public function addOrModify() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'PostCommentsService.class.php';
		if (! CommonBase::isPost ()) {
			$this->smarty->display ( "admin/ad/adsite.tpl" );
			return;
		}
		$service = new PostCommentsService ( $this->getDB () );
		$id = $_POST ["id"];
		if (empty ( $id )) {
			$input_data = array (
					'user_name' => $_POST ["user_name"],
					'passwd' => md5 ( $_POST ["passwd"] ),
					'real_name' => $_POST ["real_name"],
					'mobile' => $_POST ["mobile"],
					'email' => $_POST ["email"],
					'user_desc' => $_POST ["user_desc"],
					'role_id' => $_POST ["role_id"],
					'user_image' => $_POST ["user_image"] 
			);
			$user_id = $service->addAdUser ( $input_data );
		} else {
			$user = $service->getAdminByID ( $id );
			if (! empty ( $user )) {
				$passwd = $user->passwd;
				if ($_POST ["passwd"] != $passwd) {
					$input_data = array (
							'user_name' => $_POST ["user_name"],
							'passwd' => md5 ( $_POST ["passwd"] ),
							'real_name' => $_POST ["real_name"],
							'mobile' => $_POST ["mobile"],
							'email' => $_POST ["email"],
							'user_desc' => $_POST ["user_desc"],
							'role_id' => $_POST ["role_id"],
							'user_image' => $_POST ["user_image"] 
					);
				} else {
					$input_data = array (
							'user_name' => $_POST ["user_name"],
							'real_name' => $_POST ["real_name"],
							'mobile' => $_POST ["mobile"],
							'email' => $_POST ["email"],
							'user_desc' => $_POST ["user_desc"],
							'role_id' => $_POST ["role_id"],
							'user_image' => $_POST ["user_image"] 
					);
				}
				
				$input_condition = array (
						'id' => $_POST ["id"] 
				);
				
				$user_id = $service->edit ( $input_data, $input_condition );
			}
		}
		echo "1";
	}
	public function check() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'PostCommentsService.class.php';
		$service = new PostCommentsService ( $this->getDB () );
		$input_data = array (
				'comment_approved' => $_POST ['check'] 
		);
		$input_condition = array (
				'comment_id' => $_POST ["id"] 
		);
		$user_id = $service->edit ( $input_data, $input_condition );
		echo 1;
	}
	public function delete() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'PostCommentsService.class.php';
		$service = new PostCommentsService ( $this->getDB () );
		$id = $_POST ['id'];
		$user_id = $service->deleteUser ( $id );
		echo 1;
	}
	function test($username, $password) {
		echo 'this is a test function ,username:' . $username . ',password:' . $password;
	}
function getpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'PostCommentsService.class.php';
		// START 数据库查询及分页数据
		$service = new PostCommentsService( $this->getDB () );
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$comment_start_date	 =trim ($_POST ['comment_start_date']);
		$comment_approved =  trim($_POST ['comment_approved']);
		$comment_end_date = trim($_POST ['comment_end_date']);
		$condition = $this->getCondition ( $comment_approved, $comment_start_date, $comment_end_date );
		$offset = ($page - 1) * $rows;
		$result = array ();
	
		$row_count = $service->countNum ();
		$result ["total"] = $row_count;
		$resource_infos = $service->resourcePage ( $offset, $rows, $condition );
		$result ["rows"] = $resource_infos;
		echo json_encode ( $result );
	}
	function getCondition($comment_approved, $comment_start_date, $comment_end_date) {
		$condition = array ();
		if ($comment_approved !=null) {
			$condition ['comment_approved'] = "comment_approved = " . $comment_approved . "";
		}
		if (! empty ( $comment_start_date )) {
			$condition ['$comment_start_date'] = "comment_date >= '" . $comment_start_date . "'";
		}
		if (! empty ( $comment_end_date )) {
			$condition ['$comment_end_date'] = "comment_date <= '" . $comment_end_date . "'";
		}
		 
		$i = 0;
		foreach ( $condition as $key => $value ) {
				
			if ($i == count ( $condition ) - 1) {
				$conditions .= $value;
			} else {
				$conditions .= $value . " and ";
			}
			$i ++;
		}
		return $conditions;
	}
}